<template>
  <el-dialog @close="close" v-model="dialogVisible" width="50%">
    <template #title>
      <span class="el-dialog__title">{{ title }}</span>
    </template>
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="100px" :rules="rules">
      <el-form-item label="参数名称" prop="configName">
        <el-input v-model="ruleForm.configName" placeholder="请输入参数名称" />
      </el-form-item>
      <el-form-item label="参数键名" prop="configKey">
        <el-input v-model="ruleForm.configKey" placeholder="请输入参数键名" />
      </el-form-item>
      <el-form-item label="参数键值" prop="configValue">
        <el-input v-model="ruleForm.configValue" placeholder="请输入参数键值" />
      </el-form-item>
      <el-form-item label="系统内置" prop="configType">
        <el-radio-group v-model="ruleForm.configType">
          <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value"
            >{{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleClose(ruleFormRef)">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { ElMessageBox, ElMessage, FormInstance } from 'element-plus'
  import { getCurrentInstance, onBeforeMount, onMounted, reactive, ref } from 'vue'
  import { reqPostAddUrl, reqPostEditUrl } from '@/api/system/post'
  import { reqAddConfig, reqGetConfig, reqUpdateConfig } from '@/api/system/config'

  const { proxy }: any = getCurrentInstance()
  const ruleFormRef = ref<FormInstance>()
  const dialogVisible = ref<boolean>(false)
  const title = ref('新增参数')
  let $emit = defineEmits(['getList'])
  const rules = ref({
    configName: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
    configKey: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
    configValue: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }],
  })
  /*form表单*/
  const ruleForm = reactive({
    configId: undefined,
    configName: undefined,
    configKey: undefined,
    configValue: undefined,
    configType: 'Y',
    remark: undefined,
  })

  /*对话框关闭时的调用*/
  function close() {
    ruleFormRef.value.resetFields() /*表单重置*/
    Object.keys(ruleForm).forEach((key) => {
      if (key === 'configType') ruleForm[key] = 'Y'
      else ruleForm[key] = null
    })
  }

  const show = async (item: any = {}) => {
    title.value = '新增参数'
    /*编辑用户的操作*/
    if (item.configId) {
      title.value = '编辑参数'
      // 进行查询获取值
      await reqGetConfig(item.configId)
        .then((req: any) => {
          if (req.code === 200) {
            /*表单赋值*/
            Object.keys(req.data).forEach((key) => {
              ruleForm[key] = item[key]
            })
            // 打开对话框
            dialogVisible.value = true
          } else {
            return
          }
        })
        .catch(() => {
          return
        })
    }
    dialogVisible.value = true
  }

  const handleClose = async (done: () => void) => {
    await ruleFormRef.value.validate(async (valid: any, fields: any) => {
      if (valid) {
        if (!ruleForm.postId) {
          await reqAddConfig(ruleForm).then((req: any) => {
            if (req.code === 200) {
              proxy.$modal.msgSuccess('添加成功！')
              dialogVisible.value = false
              $emit('getList')
            } else {
              proxy.$modal.msgError(req.message != null ? req.message : '添加失败！')
              return
            }
          })
        } else {
          await reqUpdateConfig(ruleForm).then((req: any) => {
            if (req.code === 200) {
              proxy.$modal.msgSuccess('修改成功！')
              dialogVisible.value = false
              $emit('getList')
            } else {
              proxy.$modal.msgError(req.message != null ? req.message : '修改失败！')
              return
            }
          })
        }
      } else {
        console.log('error submit!', fields)
      }
    })
  }

  defineExpose({
    show,
  })
</script>

<script lang="ts">
  export default {
    dicts: ['sys_yes_no'],
  }
</script>
<style scoped lang="scss">
  @import '../index';
</style>
